"""
  Created by Amor on 2018-09-06
"""

__author__ = '骆杨'


class Scope(object):

    allow_api = []
    allow_module = []

    forbidden = []

    def __add__(self, scope):
        self.allow_api = self.allow_api + scope.allow_api
        self.allow_api = list(set(self.allow_api))
        self.allow_module = self.allow_module + scope.allow_module
        self.allow_module = list(set(self.allow_module))
        return self


class AdminScope(Scope):
    allow_api = ['get_user_list']
    allow_module = ['v1.user']

    def __init__(self):
        self + UserScope()


class UserScope(Scope):
    allow_api = ['get_user', 'delete_user']
    allow_module = []


def is_in_scope(scope, endpoint):
    scope = globals()[scope]()
    splits = endpoint.split('+')
    red_name = splits[0]
    api_name = splits[1]
    if api_name in scope.forbidden:
        return False
    if red_name in scope.allow_module:
        return True
    if api_name in scope.allow_api:
        return True
    else:
        return False
